package com.example.demo.controller;

import com.example.demo.filter.SensitiveWordFilter;
import com.example.demo.model.ChatMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

@Controller
public class ChatController {
    @Autowired
    private SimpMessagingTemplate messagingTemplate;
    @Autowired private SensitiveWordFilter filter;

    @MessageMapping("/chat/send")
    public void sendMessage(@Payload ChatMessage msg) {
        msg.setContent(filter.filter(msg.getContent()));

        String destination = msg.isGroup() ? "/topic/course-" + msg.getCourseId() : "/queue/user-" + msg.getTo();
        messagingTemplate.convertAndSend(destination, msg);
    }
}